<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>H+ 后台主题UI框架 - 基本表单</title>
    <meta name="keywords" content="H+后台主题,后台bootstrap框架,会员中心主题,后台HTML,响应式后台">
    <meta name="description" content="H+是一个完全响应式，基于Bootstrap3最新版本开发的扁平化主题，她采用了主流的左右两栏式布局，使用了Html5+CSS3等现代技术">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="css/bootstrap.min.css-v=3.3.5.css"
          tppabs="http://www.zi-han.net/theme/hplus/css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="css/font-awesome.min.css-v=4.4.0.css"
          tppabs="http://www.zi-han.net/theme/hplus/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/iCheck/custom.css"
          tppabs="http://www.zi-han.net/theme/hplus/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="css/animate.min.css" tppabs="http://www.zi-han.net/theme/hplus/css/animate.min.css"
          rel="stylesheet">
    <link href="css/style.min.css-v=4.0.0.css" tppabs="http://www.zi-han.net/theme/hplus/css/style.min.css?v=4.0.0"
          rel="stylesheet">
    <base target="_self">
    <link rel="stylesheet" type="text/css" href="./layui/css/layui.css"/>
    <style type="text/css">
        .zhuti::before {
            background: url('img/lg.jpg') no-repeat fixed center;
            background-size: 300px;
            content: "";
            opacity: 0.2;
            /*透明度设置*/
            width: 80%;
            height: 80%;
            position: absolute;
        }
    </style>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class=" zhuti ibox float-e-margins ">
                <div class="ibox-title">
                    <h3 style="font-weight: 600;margin-top: 0;">物品修改/删除</h3>
                </div>
                <div class="alert alert-danger" style="margin-bottom: 0;text-align: center;">
                    一次请修改一种物品！！！
                </div>

                <div class="ibox-content">
                    <div class="form-horizontal">

                    </div>
                </div>


                <script src="js/jquery.min.js-v=2.1.4"
                        tppabs="http://www.zi-han.net/theme/hplus/js/jquery.min.js?v=2.1.4"></script>
                <script src="js/bootstrap.min.js-v=3.3.5"
                        tppabs="http://www.zi-han.net/theme/hplus/js/bootstrap.min.js?v=3.3.5"></script>
                <script src="js/content.min.js-v=1.0.0"
                        tppabs="http://www.zi-han.net/theme/hplus/js/content.min.js?v=1.0.0"></script>
                <script src="js/plugins/iCheck/icheck.min.js"
                        tppabs="http://www.zi-han.net/theme/hplus/js/plugins/iCheck/icheck.min.js"></script>

                <script type="text/javascript" src="../../../tajs.qq.com/stats-sId=9051096"
                        tppabs="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
                <script src="./layui/layui.js" type="text/javascript" charset="utf-8"></script>

                <script>
                    let thingId = []
                    let sizeId = []
                    let sizeNum = []
                    let people = ''
                    let college = ''
                    let phone = ''
                    let tip = ''
                    let startTime = ''
                    let endTime = ''
                    let thingList=[]
                    let thingNum=0;
                    let thingRenum=0;
                    let changeNum=0;
                    //页面入口函数
                    $(document).ready(function () {
                        show();
                        initialize();
                    })

                    //发送数据
                    function send() {
                        console.log(thingList)
                        //所有输入框不为空
                        let inputG=$('input')
                        inputG.each(function () {
                            // console.log($(this))
                            if($(this)[0].value==''){
                                $(this).focus();
                                layer.alert("输入框不能为空");
                                return
                            }
                        })
                        let data={
                            id:thingList[0].tid,
                            num:parseInt(thingNum)+changeNum,
                            renum:parseInt(thingRenum)+changeNum,
                            thingSizeList:thingList
                        }
                        console.log(data)
                        $.ajax({
                            type: "POST",
                            url: "user/updateThing",
                            contentType:'application/json',
                            data:JSON.stringify(data),
                            success: function (msg) {
                                console.log(msg)
                                layer.alert(msg);
                            },
                            error: function (nn) {
                                layer.alert(nn);
                                console.log(nn);
                            }
                        });
                       /* getMsg()
                        console.log(notEmpty())
                        if (notEmpty()) {

                        } else {
                            console.log(111)
                            layer.alert('具体信息不能为空')
                            thingId = []
                            sizeId = []
                            sizeNum = []
                            return
                        }
                        console.log(people)
                        console.log(college)
                        console.log(phone)
                        console.log(tip)
                        console.log(new Date(startTime))
                        console.log(new Date(endTime))
                        console.log(thingId)
                        console.log(sizeId)
                        console.log(sizeNum)
                        $.ajax({
                            type: "POST",
                            url: "user/borrowThings",
                            data: {
                                mes: tip,
                                applicant: people,
                                college: college,
                                tel: phone,
                                starttime: new Date(startTime),
                                endtime: new Date(endTime),
                                tid: thingId,
                                sid: sizeId,
                                num: sizeNum
                            },
                            success: function (msg) {
                                // 申请成功，注意查看申请状态！
                                console.log(123)
                                console.log(msg)
                                layer.alert(msg)
                                thingId = []
                                sizeId = []
                                sizeNum = []
                                if (msg == '申请成功，注意查看申请状态！')
                                    $('button').hide();
                            },
                            fail: function () {
                                console.log('失败')
                            },
                            finally: function () {
                                console.log('结束')
                            }
                        });*/
                    }

                    //获取数据
                    function getMsg() {
                        let list = $('input')
                        console.log(list)
                        for (let i = 0; i < list.length - 6; i++) {
                            if (list[i].value != "") {
                                thingId.push(list[i].attributes[1].value)
                                sizeId.push(list[i].attributes[0].value)
                                sizeNum.push(list[i].value)
                            }
                        }
                        people = $('#sqr').val()
                        college = $('#ssxy').val()
                        phone = $('#lxdh').val()
                        tip = $('#bzxx').val()
                        startTime = $('#test1').val()
                        endTime = $('#test2').val()
                    }

                    //页面展示
                    function show() {
                        $.ajax({
                            type: "POST",
                            url: "/user/getThings",
                            data: {},
                            success: function (msg) {
                                console.log("===============")
                                console.log(msg)
                                for (let i = 0; i < msg.length; i++) {
                                    //计算物品的renum
                                    let sizeList=msg[i].list
                                    let sizeRenum=0;
                                    sizeList.forEach(function (a) {
                                        sizeRenum+=a.renum
                                    })

                                    addName(msg[i].name)
                                    let content = '<div class="form-group">'
                                    let list = msg[i].list;
                                    for (let j = 0; j < list.length; j++) {
                                        content += addSize(list[j].size, list[j].renum, msg[i].id, list[j].id,list[j].num,msg[i].num,sizeRenum)
                                    }
                                    content = content + '</div>';
                                    $('.form-horizontal').append(content);
                                }
                                addSubmit();
                                initialize();
                            }
                        });
                    }

                    //添加物品名称
                    function addName(name) {
                        let nameString = '<div class="ibox-title">\n' +
                            '                            <h3 style="font-weight: 600;margin-top: 0;padding-right: 500px;">\n' +
                            '                                ' + name + ':</h3>\n' +
                            '                        </div>'
                        $('.form-horizontal').append(nameString);
                    }

                    //添加物品的一个规格
                    function addSize(size, renum, id_thing, id_size,num,thingNum,thingRenum) {
                        let item = ' <label class="col-sm-3 control-label">' + size + ':</label>\n' +
                            '                            <div class="col-sm-2 row" style="margin-bottom: 14px;">\n' +
                            '                                <input data-mes='+size+'  onchange="inputChange(this)" value='+num+' data-size=' + id_size + ' data-thing=' + id_thing + ' min="0" max=' + renum + ' type="number" placeholder="<=' + renum + '" class="form-control" data-thingNum='+thingNum+' data-thingRenum='+thingRenum+'>\n' +
                            '<butoon onclick="deleteSize('+id_size+')" class="btn-danger btn-xs">删除</butoon>\n'+
                            '                            </div>'
                        return item;
                    }

                    //删除一个物品规格
                    function deleteSize(id) {
                        console.log(id.toString())
                        $.ajax({
                            type: "POST",
                            url: "user/deleteThingSize",
                            // contentType:'application/json',
                            data: {
                                sId:id.toString()
                            },
                            success: function (msg) {
                                console.log(msg)
                                layer.alert(msg);
                                setTimeout(function() { window.location.href = "thingsChange.html"; }, 2000 );
                            },
                            error: function (nn) {
                                layer.alert(nn);
                                console.log(nn);
                            }
                        });
                    }

                    //输入框修改
                    function inputChange(mes) {
                        console.log($(mes))
                        let x=$(mes)[0]
                        thingNum=x.attributes[10].value
                        thingRenum=x.attributes[11].value
                        let item={
                            id:parseInt(x.attributes[3].value),
                            tid:parseInt(x.attributes[4].value),
                            size:x.attributes[0].value,
                            num:parseInt(x.value),
                            renum:parseInt(x.attributes[6].value)+(parseInt(x.value)-parseInt(x.attributes[2].value))
                        }
                        changeNum=parseInt(x.value)-parseInt(x.attributes[2].value)
                        let flag=-1;
                        thingList.forEach(function (a) {
                            if (a.id==item.id){
                                flag=$.inArray(a,thingList);
                            }
                        })
                        if(flag!=-1){
                            thingList.splice(flag,1);
                        }
                        thingList.push(item)
                        flag=0;
                        console.log(thingList)
                    }

                    //    添加提交按钮
                    function addSubmit() {
                        let button = '<div class="form-group" style="border-top: 4px solid #E7EAEC;">\n' +
                            '                            <div class="col-sm-3 col-sm-offset-4" style="margin-top: 20px;">\n' +
                            '                                <button onclick="send()" class="btn btn-primary" type="submit"\n' +
                            '                                        style="margin-right: 20%;">修改完成</button>\n' +
                            '                                <button class="btn btn-white" type="reset">重新填写</button>\n' +
                            '                            </div>\n' +
                            '                        </div>'
                        $('.form-horizontal').append(button)
                    }

                    //    添加详细信息
                    function addDetail() {
                        let detail = '<div class="form-group" style="margin-bottom: 0px;">\n' +
                            '                            <label class="col-sm-2 control-label">申请人</label>\n' +
                            '                            <!-- col-sm-4 m-l-n -->\n' +
                            '                            <div class="col-sm-3">\n' +
                            '                                <input id="sqr"  type="text" name="" required placeholder="请输入申请人"\n' +
                            '                                       class="form-control">\n' +
                            '                            </div>\n' +
                            '                            <label class="col-sm-2 control-label">所属学院</label>\n' +
                            '                            <div class="col-sm-4">\n' +
                            '                                <input id="ssxy" type="text" name="meetingname" required placeholder="请输入所属学院"\n' +
                            '                                       class="form-control">\n' +
                            '                            </div>\n' +
                            '                        </div><br>\n' +
                            '<div class="form-group" style="margin-bottom: 0px;">\n' +
                            '                            <label class="col-sm-2 control-label">联系电话</label>\n' +
                            '                            <!-- col-sm-4 m-l-n -->\n' +
                            '                            <div class="col-sm-3">\n' +
                            '                                <input id="lxdh" type="text" name="" required placeholder="请输入联系电话"\n' +
                            '                                       class="form-control">\n' +
                            '                            </div>\n' +
                            '                            <label class="col-sm-2 control-label">借用原因</label>\n' +
                            '                            <div class="col-sm-4">\n' +
                            '                                <input id="bzxx" type="text" name="" required placeholder="请输入借用原因"\n' +
                            '                                       class="form-control">\n' +
                            '                            </div>\n' +
                            '                        </div><br>\n' +
                            '                        <div class="form-group" id="laydate">\n' +
                            '                            <label class="col-sm-2 control-label">开始时间</label>\n' +
                            '                            <div class="col-sm-3">\n' +
                            '                                <input type="text" name="starttime" required placeholder="yyyy-MM-dd HH:mm:ss"\n' +
                            '                                       class="form-control"\n' +
                            '                                       id="test1">\n' +
                            '                            </div>\n' +
                            '                            <label class="col-sm-2 control-label">结束时间</label>\n' +
                            '                            <div class="col-sm-4">\n' +
                            '                                <input disabled="disabled" type="text" name="endtime" required placeholder="yyyy-MM-dd HH:mm:ss"\n' +
                            '                                       class="form-control"\n' +
                            '                                       id="test2">\n' +
                            '                            </div>\n' +
                            '                        </div>'
                        $('.form-horizontal').append(detail)
                    }

                    //获取时间
                    function getNowTime() {
                        var myDate = new Date();
                        //获取当前年
                        var year = myDate.getFullYear();
                        //获取当前月
                        var month = myDate.getMonth() + 1;
                        //获取当前日
                        var date = myDate.getDate();
                        var h = myDate.getHours(); //获取当前小时数(0-23)
                        var m = myDate.getMinutes(); //获取当前分钟数(0-59)
                        var s = myDate.getSeconds();
                        let now = year + '-' + p(month) + "-" + p(date) + " " + p(h) + ':' + p(m) + ":" + p(s);
                        return now;
                    }

                    function p(s) {
                        return s < 10 ? '0' + s : s;
                    }

                    //    初始化时间日期选择器
                    function initialize() {
                        layui.use('laydate', function () {
                            var laydate = layui.laydate;
                            laydate.render({
                                elem: '#test1',
                                type: 'datetime',
                                min: getNowTime(),
                                done: function (value, date, endDate) {
                                    $('#test2').attr('disabled', false)
                                    if (value != '') {
                                        endTime.config.min = {
                                            year: date.year,
                                            month: date.month - 1,//关键
                                            date: date.date,
                                            hours: date.hours,
                                            minutes: date.minutes,
                                            seconds: date.seconds
                                        };
                                        endTime.config.max = {
                                            year: date.year,
                                            month: date.month - 1,//关键
                                            date: date.date + 1,
                                            hours: date.hours,
                                            minutes: date.minutes,
                                            seconds: date.seconds
                                        };
                                    } else {
                                        endTime.config.min = {
                                            year: 2200,
                                            month: 0,
                                            date: 1,
                                        };
                                    }
                                    ;
                                }
                            });
                            var endTime = laydate.render({
                                elem: '#test2',
                                type: 'datetime',
                                btns: ['now', 'confirm'],
                                min: 0,
                                max: 100
                            });
                        })
                    }

                    //加一天
                    function addDate(time) {
                        var timestamp = Date.parse(new Date(time));
                        timestamp = timestamp / 1000;
                        timestamp += 86400;//加一天
                        var newTime = new Date(timestamp * 1000).format('yyyy-MM-dd hh:mm:ss');
                        return newTime;
                    }

                    //日期格式
                    Date.prototype.format = function (format) {
                        var date = {
                            "M+": this.getMonth() + 1,
                            "d+": this.getDate(),
                            "h+": this.getHours(),
                            "m+": this.getMinutes(),
                            "s+": this.getSeconds(),
                            "q+": Math.floor((this.getMonth() + 3) / 3),
                            "S+": this.getMilliseconds()
                        };
                        if (/(y+)/i.test(format)) {
                            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
                        }
                        for (var k in date) {
                            if (new RegExp("(" + k + ")").test(format)) {
                                format = format.replace(RegExp.$1, RegExp.$1.length == 1
                                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                            }
                        }
                        return format;
                    }

                    //    详细信息非空
                    function notEmpty() {
                        let msg = false;
                        if (people == '' || college == '' || phone == '' || tip == '' || startTime == '' || endTime == '') {
                        } else {

                            msg = true
                        }
                        return msg
                    }

                </script>
</body>

</html>
